package cp3120.net;

import cp3120.model.User;
import generic.net.Peer;
import generic.net.client.Client;
import generic.net.observers.PeerConnectObserver;
import java.util.List;

/**
 * A Singleton that represents a clients connection to a server.
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class AirportClient extends Client implements PeerConnectObserver, UserProvider {
    private static AirportClient instance;
    private User user;

    public AirportClient() {
        getPeerConnectDispatcher().add(this);
        ActiveUser.setProvider(this);
    }

    public static AirportClient getInstance() {
        if (instance == null) {
            instance = new AirportClient();
        }
        return instance;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getActiveUsers() {
        return (List<User>) sendRequest("AccountManagerService", "activeUserList").getResponse().getPayload();
    }

    public void peerConnect(Peer peer) {
        // Let the server tell us who we are by default, and what permissions we have.
        user = (User)sendRequest("AccountManagerService", "whoAmI").getResponse().getPayload();
        System.out.println("[Airport Client] Connected as " + user);
    }
}
